<?php

namespace App\Exports;


use App\Models\Constant\DevConstant;
use App\Models\Dev\DevInfo;
use App\Models\User\User;
use Illuminate\Support\Facades\Log;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

class DemoExport extends CommonExport
{
    public function __construct(array $conditions=[],array $params=[]) {
        $this->instance = (new DevInfo());
        $this->conditions = $conditions;
        $this->params = $params;
        $this->title = [
            'ID',
            '编码',
            '业务类型',
            '设备类型',
            '设备名称',
            '状态',
            '矿',
            '区域',
            '地点',
            '备注',
            '是否在用',
            '使用记录'
        ];
    }

    public function array(): array {
        $this->records = [$this->title];
        //$cols = ['id', 'code', 'name', 'status', 'type_id', 'coal', 'region', 'local', 'beizhu', 'busy',];
        $cols = ['*'];

        $datas = $this->instance->getLst($this->conditions)
            ->with(['getType','getLog'])->get($cols);
        foreach ($datas as $data) {
            $temp = [
                $data->id,
                $data->code,
                DevConstant::MAP_BUSINESS_NAME[$data->getType->business_type]??'未知',
                $data->getType->name,
                $data->name,
                $data->status_name,
                $data->coal,
                $data->region,
                $data->local,
                $data->beizhu,
                $data->busy,
            ];
            $logs = [];
            foreach ($data->getLog as $log) {
                $oper = $log->oper_type==1?'安装':'回收';
                $strLog = implode(',', [$oper,$log->coal,$log->region,$log->local, $log->date,$log->people,$log->beizhu]);
                $logs[] = $strLog;
            }

            $temp[] = implode('|', $logs);
            $this->records[] = $temp;
        }
        //dd($this->records);

        return $this->records;
    }

    /*public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }*/

}
